<template>
  <div class="race page">
    <Title />
    <div class="camera bg-full animate__animated animate__zoomIn">
      <input type="file" accept="image/*" @change="handleUpload" />
    </div>
    <div class="btn bg-full animate__animated animate__pulse animate__infinite">
      <input type="file" @change="handleUpload" />
    </div>
    <div class="tips">注意：请您上传个人真实比武照片</div>
  </div>
</template>

<script setup>
import Title from '@/components/Title.vue'
import { uploadFile } from '@/utils.js'
import { updateRaceUrl } from '@/api/index.js'
import { useGlobalStore } from '@/stores/global.js'
import { toast } from 'vue3-toastify'

const globalStore = useGlobalStore()

const handleUpload = async (event) => {
  const [file] = event.target.files
  if (file.size > 10 * 1024 * 1024) {
    toast('上传图片大小不能超过10M')
    return
  }
  const loading = toast.loading('上传中...')
  try {
    const result = await uploadFile(file)
    await updateRaceUrl(globalStore.user.id, result.url)
    globalStore.setUser({
      raceUrl: result.url
    })
    toast('上传成功')
  } finally {
    toast.remove(loading)
  }
}
</script>

<style lang="less" scoped>
.camera {
  width: 315px;
  height: 315px;
  background-image: url(../assets/img/race/camera.png);
  margin-top: 210px;
}

.btn {
  width: 829px;
  height: 137px;
  background-image: url(../assets/img/race/btn.png);
  margin-top: 120px;
}

.btn,
.camera {
  position: relative;

  input {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    opacity: 0;
  }
}

.tips {
  color: #ff0000;
  margin-top: 30px;
  font-size: 50px;
}
</style>
